package main

import (
<<<<<<< HEAD
	"encoding/json"
	"fmt"
	"hwanginsitein/network"
	"net"
=======
	"fmt"
	"net"
	"hwanginsitein/network"
	"encoding/json"
>>>>>>> 5ff4299df15cebcc96b503c1bef32f6520030e85
)

const connectType = network.Network
const port = network.Port
const connectAddress = "0.0.0.0:" + port

func main() {
<<<<<<< HEAD
	listen, err := net.Listen(connectType, connectAddress)
	defer func() {
		if err := recover(); err != nil {
=======
	listen, err := net.Listen(connectType,connectAddress)
	defer func() {
		if err := recover();err != nil {
>>>>>>> 5ff4299df15cebcc96b503c1bef32f6520030e85
			listen.Close()
		}
	}()
	defer listen.Close()
	if err != nil {
		listen.Close()
		return
	}
	for {
		conn, connErr := listen.Accept()
		if connErr != nil {
			fmt.Println(connErr)
			return
		}
		defer conn.Close()
		go receive(conn)
	}
}

func receive(conn net.Conn) {
	defer func() {
<<<<<<< HEAD
		if err := recover(); err != nil {
=======
		if err := recover();err != nil {
>>>>>>> 5ff4299df15cebcc96b503c1bef32f6520030e85
			conn.Close()
		}
	}()
	for {
		buf := make([]byte, 8096)
		n, err := conn.Read(buf)
		if err != nil {
			break
		}
		var v interface{}
<<<<<<< HEAD
		errJson := json.Unmarshal(buf[:n], &v)
=======
		errJson := json.Unmarshal(buf[:n],&v)
>>>>>>> 5ff4299df15cebcc96b503c1bef32f6520030e85
		if errJson != nil {
			fmt.Println(err)
			break
		}
		fmt.Println(v)
	}
}
